package com.stu.view;

import com.stu.dao.StudentDao;
import com.stu.model.Student;
import com.stu.util.StringUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;

/**
 * @author Fairy
 */
public class StudentsList extends JInternalFrame {
	private final JTextField updateIdTextField;
	private final JTextField updateScoreTextField;
	private final JTextArea textArea;

	/**
	 * 布局
	 */
	public StudentsList() {
		setTitle("学生列表");
		setBounds(100, 100, 643, 381);
		getContentPane().setLayout(null);
		setClosable(true);
		//内部窗体添加关闭最小化
		setIconifiable(true);
		
		JPanel btnPanel = new JPanel();
		btnPanel.setBounds(437, 47, 184, 249);
		getContentPane().add(btnPanel);
		btnPanel.setLayout(null);
		
		JButton updateButton = new JButton("修改");
		updateButton.addActionListener(this::updateStu);
		updateButton.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
		updateButton.setBounds(33, 109, 113, 27);
		btnPanel.add(updateButton);
		
		JButton delButton = new JButton("删除");
		delButton.addActionListener(this::delStu);
		delButton.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
		delButton.setBounds(33, 176, 113, 27);
		btnPanel.add(delButton);
		
		JButton queryButton = new JButton("查询");
		queryButton.addActionListener(this::queryList);
		queryButton.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
		queryButton.setBounds(33, 46, 113, 27);
		btnPanel.add(queryButton);
		
		textArea = new JTextArea();
		textArea.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
		textArea.setBounds(14, 13, 275, 319);
		getContentPane().add(textArea);
		
		JPanel updatePanel = new JPanel();
		updatePanel.setBounds(301, 47, 122, 230);
		getContentPane().add(updatePanel);
		updatePanel.setLayout(null);
		
		JLabel updateNameLabel = new JLabel("姓名：");
		updateNameLabel.setIcon(new ImageIcon(Objects.requireNonNull(StudentsList.class.getResource("/img/姓名.png"))));
		updateNameLabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
		updateNameLabel.setBounds(14, 13, 72, 18);
		updatePanel.add(updateNameLabel);

		JTextField updateNameTextField = new JTextField();
		updateNameTextField.setBounds(14, 44, 86, 24);
		updatePanel.add(updateNameTextField);
		updateNameTextField.setColumns(15);
		
		JLabel updateIdLabel = new JLabel("学号：");
		updateIdLabel.setIcon(new ImageIcon(Objects.requireNonNull(StudentsList.class.getResource("/img/姓名.png"))));
		updateIdLabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
		updateIdLabel.setBounds(14, 81, 72, 18);
		updatePanel.add(updateIdLabel);

		updateIdTextField = new JTextField();
		updateIdTextField.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
		updateIdTextField.setBounds(14, 112, 86, 24);
		updatePanel.add(updateIdTextField);
		updateIdTextField.setColumns(15);
		
		JLabel updateScoreLabel = new JLabel("成绩：");
		updateScoreLabel.setIcon(new ImageIcon(Objects.requireNonNull(StudentsList.class.getResource("/img/成绩.png"))));
		updateScoreLabel.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
		updateScoreLabel.setBounds(14, 149, 72, 18);
		updatePanel.add(updateScoreLabel);

		updateScoreTextField = new JTextField();
		updateScoreTextField.setBounds(14, 180, 86, 24);
		updatePanel.add(updateScoreTextField);
		updateScoreTextField.setColumns(15);

	}

	/**
	 * 删除学生
	 */
	protected void delStu(ActionEvent e) {
		String name = updateScoreTextField.getText();
		String id = updateScoreTextField.getText();
		String score = updateScoreTextField.getText();
		if (StringUtil.isEmpty(name)) {
			JOptionPane.showMessageDialog(this, "姓名不能为空！");
			return;
		}
		if (StringUtil.isEmpty(id)) {
			JOptionPane.showMessageDialog(this, "学号不能为空！");
			return;
		}
		if (!StringUtil.isEmpty(score)) {
			JOptionPane.showMessageDialog(this, "删除操作请勿填写成绩！");
			updateScoreTextField.setText("");
			return;
		}
		Student student = new Student();
		student.setId(id);
		student.setName(name);
		
		StudentDao studentDao = new StudentDao();
		if (studentDao.del(student)) {
			JOptionPane.showMessageDialog(this, "删除成功！");
		}else {
			JOptionPane.showMessageDialog(this, "删除失败！");
		}
	}

	/**
	 * 修改信息
	 */
	protected void updateStu(ActionEvent e) {
		String name = updateIdTextField.getText();
		String id = updateIdTextField.getText();
		String score = updateScoreTextField.getText();
		if (StringUtil.isEmpty(name)) {
			JOptionPane.showMessageDialog(this, "姓名不能为空！");
			return;
		}
		if (StringUtil.isEmpty(id)) {
			JOptionPane.showMessageDialog(this, "学号不能为空！");
			return;
		}
		if (StringUtil.isEmpty(score)) {
			JOptionPane.showMessageDialog(this, "成绩不能为空！");
			return;
		}
		Student student = new Student();
		student.setId(id);
		student.setName(name);
		student.setScore(score);
		
		StudentDao studentDao = new StudentDao();
		if (studentDao.update(student)) {
			JOptionPane.showMessageDialog(this, "更改成功！");
		}else {
			JOptionPane.showMessageDialog(this, "更改失败！请检查信息是否填写正确！");
		}
	}

	/**
	 * 查询
	 */
	protected void queryList(ActionEvent e) {
		StudentDao studentDao = new StudentDao();
		textArea.setText("查询全部的结果如下：\n");
		textArea.append("学号|姓名|成绩\n");
		ResultSet rss = studentDao.query();
		String id,name,score;
		try {
			while (rss.next()) {
				id = rss.getString(1);
				name=rss.getString(2);
				score=rss.getString(3);
				textArea.append(id+" | ");
				textArea.append(name+" | ");
				textArea.append(score+"\n");
			}
			JOptionPane.showMessageDialog(this, "查询成功！");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "查询失败！");
			e1.printStackTrace();
		}
	}
}


